package com.sql.azarias.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.sql.azarias.AppManager;
import com.sql.azarias.http.AbstractRequestNew;
import com.sql.azarias.http.CallServer;
import com.sql.azarias.http.HttpListener;

public abstract class BaseMvpActivity<V,T extends BasePresenter<V>> extends AppCompatActivity {

    public T presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseConfig();
        setContentView(getLayoutId());
        initInjector();
        presenter = initPresenter();


        initEventAndData();
        presenter.attach((V)this);
    }
    protected <T> void request(Context activity, AbstractRequestNew<T> request, HttpListener<T> httpListener ) {
        CallServer.getInstance().add(activity,0, request, httpListener);
    }

    protected <T> void request(Context activity , int what, AbstractRequestNew<T> request, HttpListener<T> httpListener) {
        CallServer.getInstance().add(activity,what, request, httpListener);
    }
    private void setBaseConfig() {
        //记录每次打开的activity
        AppManager.getAppManager().addActivity(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettach();

        AppManager.getAppManager().finishActivity(this);
    }


    public abstract T initPresenter();
    protected abstract int getLayoutId();

    protected abstract void initInjector();

    protected abstract void initEventAndData();

}
